﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web;
using System.Web.UI.WebControls;
using System.IO;
using System.Configuration;

namespace KoSoft
{
	public class UtilWeb
	{
		public static void Message(Page _page, string m, string js)
		{
			if (_page == null)
				return;
			if (!string.IsNullOrEmpty(m) && string.IsNullOrEmpty(js))
				_page.ClientScript.RegisterStartupScript(_page.GetType(), "", "<script>alert(\"" + m + "\")</script>");
			else if (string.IsNullOrEmpty(m) && !string.IsNullOrEmpty(js))
				_page.ClientScript.RegisterStartupScript(_page.GetType(), "", "<script>" + js + "</script>");
			else if (!string.IsNullOrEmpty(m) && !string.IsNullOrEmpty(js))
				_page.ClientScript.RegisterStartupScript(_page.GetType(), "", "<script>alert(\"" + m + "\");" + js + "</script>");
		}
		public static void PageJump(Page _page, string url)
		{
			if (_page == null)
				return;
			if (!string.IsNullOrWhiteSpace(url))
				_page.ClientScript.RegisterStartupScript(_page.GetType(), "", "<script>window.location.href=\"" + url + "\";</script>");
		}
		public static string GetAppSetting(string key)
		{
			return ConfigurationManager.AppSettings[key];
		}
		public static void SetCookie(Page _page, string key, string value, int days)
		{
			HttpCookie cookie = new HttpCookie(key);
			cookie.Value = value;
			cookie.Expires = DateTime.Now.AddDays(days);
			_page.Response.Cookies.Add(cookie);
		}
		public static string GetCookie(Page _page, string key)
		{
			var temp = _page.Request.Cookies[key];
			return temp == null ? null : temp.Value;
		}
		private static string GetDateTime()
		{
			return DateTime.Now.ToString("yyyyMMddhhmmss");
		}
		/// <summary>
		/// check fu hasFile before you invoke this // absolute path of the file
		/// </summary>
		/// <param name="page"></param>
		/// <param name="fu"></param>
		/// <param name="listFormat"></param>
		/// <param name="path"></param>
		/// <returns></returns>
		public static string UploadFile(Page page, FileUpload fu, List<string> listFormat, string path)
		{
			try
			{
				string file = fu.FileName;
				if (file.Trim() == "")
					throw new Exception("No file");
				string type = Path.GetExtension(file).ToLower();
				if (listFormat != null && listFormat.Count > 0)
					if (!listFormat.Contains(type))
						throw new Exception("Invalid file format" + ", only " + listFormat.ToString() + " supported.");
				file = GetDateTime() + type;
				while (true)
				{
					if (File.Exists(page.Server.MapPath(path + file)))
						file = GetDateTime() + type;
					else
						break;
				}
				fu.SaveAs(page.Server.MapPath(path + file));
				return (path + file);
			}
			catch (Exception x)
			{
				throw x;
			}
		}
		/// <summary>
		/// set ddl selected
		/// </summary>
		/// <param name="ddl"></param>
		/// <param name="value"></param>
		/// <returns></returns>
		public static bool SetDDLSelected(DropDownList ddl, string value)
		{
			try
			{
				if (ddl.Items.Count == 0)
					return false;
				foreach (ListItem item in ddl.Items)
					item.Selected = false;
				foreach (ListItem item in ddl.Items)
					if (item.Value.Equals(value))
					{
						item.Selected = true;
						return true;
					}
				return false;
			}
			catch (Exception x)
			{
				return false;
				throw x;
			}
		}
	}
}